#include <wfrest/HttpServer.h>
using namespace wfrest;
#include "cmath"
#include "cstring"

std::condition_variable cv;
std::mutex mtx;
bool stop_flag = false;

void signal_handler(int signum)
{
    std::unique_lock<std::mutex> lock(mtx);
    stop_flag = true;
    cv.notify_one();
}



int main()
{
    HttpServer svr;
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    // curl -v http://ip:port/hello
    svr.GET("/hello", [](const HttpReq *req, HttpResp *resp)
    {
        resp->String("world\n");
    });
    // curl -v http://ip:port/data
    svr.GET("/data", [](const HttpReq *req, HttpResp *resp)
    {
        std::string str = "Hello world";
        resp->String(std::move(str));
    });

    // curl -v http://ip:port/post -d 'post hello world'
    svr.POST("/post", [](const HttpReq *req, HttpResp *resp)
    {
        // reference, no copy here
        std::string& body = req->body();
        fprintf(stderr, "post data : %s\n", body.c_str());
    });

    if (svr.start(8888) == 0)
    {
        std::unique_lock<std::mutex> lock(mtx);
        cv.wait(lock, [] { return stop_flag; });
        svr.stop();
    } else
    {
        exit(1);
    }
    return 0;
}